package com.dididoctor.patient.Activity.Doctor;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.MV.BaseFragment;
import com.dididoctor.patient.R;
import com.dididoctor.patient.Ui.customlistview.OnRefreshListener;
import com.dididoctor.patient.Ui.customlistview.RefreshListView;
import com.dididoctor.patient.Utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述： 登陆页
 * @项目名称：ddgh
 * @类名称： loginActivity
 * @包名称： com.doudoukeji.service.Activity.login
 * @创建人：张强 boy
 * @创建时间：2016/3/7 16:40
 * @修改人：
 * @修改时间：2016/3/7 16:40
 * @修改备注： new parent class(用一句话描述修改了什么)
 */

@SuppressLint("ValidFragment")
public class ForumModuleFragment extends BaseFragment implements View.OnClickListener, ForumView {
    private Activity activity;
    private String longitude, latitude, isOnline, cityCode;
    private int pageIndex = 1;

    private RefreshListView listView;
    private DoctorListAdapter adapter;
    private ForumPresenter forumPresenter;
    private List<Doctor> doctors = new ArrayList<Doctor>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @SuppressLint("ValidFragment")
    public ForumModuleFragment(String isOnline, String longitude, String latitude, String cityCode) {
        this.isOnline = isOnline;
        this.longitude = longitude;
        this.latitude = latitude;
        this.cityCode = cityCode;
    }

    @Override
    public void onAttach(Activity activity) {
        this.activity = activity;
        super.onAttach(activity);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_doctor_list, null);
        this.initView(rootView);
        return rootView;

    }

    private void initView(View rootView) {
        forumPresenter = new ForumPresenter(getActivity(), this);
        forumPresenter.getdoctordata(longitude, latitude, isOnline, pageIndex, cityCode, "");

        listView = (RefreshListView) rootView.findViewById(R.id.lv_forum);
        listView.setHeadAndFoot(false, false);
        listView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onPullDownRefresh() {
                pageIndex = 1;
                forumPresenter.getdoctordata(longitude, latitude, isOnline, pageIndex, cityCode, "");
            }

            @Override
            public void onLoadMoring() {
                listView.onRefreshFinish();
            }

            @Override
            public void onScrollListener(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        listView.setHeadAndFoot(true, true);
        adapter = new DoctorListAdapter(getActivity(), doctors, longitude, latitude);
        listView.setAdapter(adapter);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    }

    @Override
    public void getdoctorfail() {
        listView.onRefreshFinish();
    }

    @Override
    public void getdoctorsucced(List<Doctor> doctors) {
        if (!Util.isEmpty(doctors)) { //如果获得的数据不为空
            listView.setVisibility(View.VISIBLE);
            if (pageIndex == 1) {
                adapter.setData(doctors);
                listView.onRefreshFinish();
            }else{
                adapter.addData(doctors);
                if(doctors.size()< ConstantValue.LOAD_MESSAGE_COUNT){
                    listView.showNoMoreData();
                }else{
                    listView.hideNoMoreData();
                }
            }
        } else {//为空
            if (pageIndex == 1) {
                listView.onRefreshFinish();
            }else{
                listView.showNoMoreData();
            }
        }
    }


}
